package com.media.revision.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.media.MediaGlideLoader;
import com.media.R;
import com.media.image.ImageModel;

import java.util.ArrayList;
import java.util.List;

public class ImagrListAdapter extends BaseAdapter {

    private List<ImageModel> datas = new ArrayList<>();
    private String fileType = ImageModel.TYPE_IMAGE;
    private LayoutInflater mInflater;
    private Activity mActivity;

    public ImagrListAdapter(List<ImageModel> datas, String fileType, LayoutInflater mInflater,Activity activity) {
        if (null != datas) {
            this.datas.addAll(datas);
        }
        this.fileType = fileType;
        this.mInflater = mInflater;
        this.mActivity = activity;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.media_view_type_image_item, null);
            viewHolder = new ViewHolder();
            viewHolder.imageIcon = convertView.findViewById(R.id.image_icon);
            viewHolder.fileName = convertView.findViewById(R.id.file_name);
            viewHolder.imageNum = convertView.findViewById(R.id.image_num);
            viewHolder.selec = convertView.findViewById(R.id.selec);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ImageModel item = datas.get(position);
        if (fileType.equals(item.fileName)) {
            viewHolder.selec.setVisibility(View.VISIBLE);
        } else {
            viewHolder.selec.setVisibility(View.GONE);
        }
        viewHolder.fileName.setText("" + item.fileName);
        viewHolder.imageNum.setText(item.pisNum + "张");
        MediaGlideLoader.LoderLoadImageType(mActivity.getApplication(), item.path, viewHolder.imageIcon);
        return convertView;
    }

    public class ViewHolder {
        private ImageView imageIcon;
        private TextView fileName;
        private TextView imageNum;
        private ImageView selec;
    }
}